package com.cms.framework.utils.page;

import java.util.List;

/**
 * 分页封装函数
 */
public class PageView<E> {
	/**
	 * 查询条件
	 */
	private E			queryCondition;

	/**
	 * 分页数据
	 */
	private List<E>		records;

	/**
	 * 页码的开始索引类 这个类包含，startindex开始索引，endindex结束索引
	 */
	private PageIndex	pageindex;

	/**
	 * 总页数
	 * 
	 */
	private long		pageCount;

	/**
	 * 每页显示几条记录
	 */
	private int			pageSize	= 10;

	/**
	 * 默认当前页为第一页
	 */
	private int			pageNow		= 1;

	/**
	 * 总记录数
	 */
	private long		rowCount;

	/**
	 * 从第几条记录开始
	 */
	private int			startPage;

	/**
	 * 规定显示5个页码
	 */
	private int			pageCode	= 5;

	private String pageStr;
	/**
	 * 要获得记录的开始索引即开始页码
	 * 
	 * @return
	 */
	public int getFirstResult() {
		return (this.pageNow - 1) * this.pageSize;
	}

	public int getPageCode() {
		return pageCode;
	}

	public void setPageCode(int pageCode) {
		this.pageCode = pageCode;
	}

	/**
	 * 使用构造函数，强制必需输入每页显示数量和当前页
	 * 
	 * @param pageSize
	 *            　　每页显示数量
	 * @param pageNow
	 *            　当前页
	 */
	public PageView(int pageSize, int pageNow) {
		this.pageSize = pageSize;
		this.pageNow = pageNow;
	}

	/**
	 * 使用构造函数，强制必需输入当前页
	 * 
	 * @param pageNow
	 *            　当前页
	 */
	public PageView(int pageNow) {
		this.pageNow = pageNow;
		startPage = (this.pageNow - 1) * this.pageSize;
	}

	/**
	 * 查询结果方法把记录数结果集合放入到PageView对象
	 * 
	 * @param rowCount
	 *            总记录数
	 * @param records
	 *            结果集合
	 */
	public void setQueryResult(long rowCount, List<E> records) {
		setRowCount(rowCount);
		setRecords(records);
	}

	public void setRowCount(long rowCount) {
		this.rowCount = rowCount;
		setPageCount(this.rowCount % this.pageSize == 0 ? this.rowCount / this.pageSize : this.rowCount / this.pageSize
				+ 1);
	}

	public List<E> getRecords() {
		return records;
	}

	public void setRecords(List<E> records) {
		this.records = records;
	}

	public PageIndex getPageindex() {
		return pageindex;
	}

	public void setPageindex(PageIndex pageindex) {
		this.pageindex = pageindex;
	}

	/**
	 * WebTool这是一个分页工具类 　
	 * <p>
	 * pagecode要获得记录的开始索引即开始页码
	 * pageNow当前页pageCount总页数这个工具类返回的是页索引PageIndex在这个方法中存在一个问题，每页显示数量和当前页、不能为空
	 * 必需输
	 * </p>
	 */
	public void setPageCount(long pageCount) {
		this.pageCount = pageCount;
		this.pageindex = getPageIndex(pageCode, pageNow, pageCount);
	}

	public int getPageNow() {
		return pageNow;
	}

	public void setPageNow(int pageNow) {
		this.pageNow = pageNow;
	}

	public long getPageCount() {
		return pageCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public long getRowCount() {
		return rowCount;
	}

	public int getStartPage() {
		return startPage;
	}

	public void setStartPage(int startPage) {
		this.startPage = startPage;
	}

	public E getQueryCondition() {
		return queryCondition;
	}

	public void setQueryCondition(E queryCondition) {
		this.queryCondition = queryCondition;
	}

	private static PageIndex getPageIndex(long pagecode, int pageNow, long pageCount) {
		long startpage = pageNow - (pagecode % 2 == 0 ? pagecode / 2 - 1 : pagecode / 2);
		long endpage = pageNow + pagecode / 2;
		if (startpage < 1) {
			startpage = 1;
			if (pageCount >= pagecode)
				endpage = pagecode;
			else
				endpage = pageCount;
		}
		if (endpage > pageCount) {
			endpage = pageCount;
			if ((endpage - pagecode) > 0)
				startpage = endpage - pagecode + 1;
			else
				startpage = 1;
		}
		return new PageIndex(startpage, endpage);
	}

	public String getPageStr() {
		StringBuffer sb = new StringBuffer();
		if (this.getRowCount() > 0) {
			sb.append("共 "+this.getRowCount()+"条&nbsp;	每页<input name=\"showCount\" value=\""+this.getPageSize()+"\" style=\"width: 30px;\" onfocus=\"this.select();\" onblur=\"jQuery.cookie('_cookie_page_size',this.value,{expires:3650});\" onkeypress=\"if(event.keyCode==13){$(this).blur();return false;}\" type=\"text\">条&nbsp;");
			if (getPageNow() == 1) {
				sb.append(" <input value=\"首 页\" onclick=\"_gotoPage('1');\" type=\"button\">");
				sb.append(" <input value=\"上一页\" onclick=\"_gotoPage('1');\" type=\"button\">");
			} else {
				sb.append(" <input value=\"首 页\" onclick=\"_gotoPage('1');\" type=\"button\">");
				sb.append(" <input value=\"上一页\" onclick=\"_gotoPage('"+(this.getPageNow()-1)+"');\" type=\"button\">");
			}
//			int showTag = 3; // 分页标签显示数量
//			int startTag = 1;
//			if (this.getPageNow() > showTag) {
//				startTag = this.getPageNow() - 1;
//			}
//			int endTag = startTag + showTag - 1;
//			for (int i = startTag; i <= this.getPageCount() && i <= endTag; i++) {
//				if (this.getPageNow() == i)
//					sb.append("<li class=\"current\">" + i + "</li>\n");
//				else
//					sb.append(" <li><a href=\"#@\" onclick=\"nextPage(" + i
//							+ ")\">" + i + "</a></li>\n");
//			}

			if (this.getPageNow() == this.getPageCount()) {
				sb.append(" <input value=\"下一页\" onclick=\"_gotoPage('"+this.getPageCount()+"');\" type=\"button\">");
				sb.append(" <input value=\"尾页\" onclick=\"_gotoPage('"+this.getPageCount()+"');\" type=\"button\">");
			} else {
				sb.append(" <input value=\"下一页\" onclick=\"_gotoPage('"+(this.getPageNow()+1)+"');\" type=\"button\">");
				sb.append(" <input value=\"尾页\" onclick=\"_gotoPage('"+this.getPageCount()+"');\" type=\"button\">");
			}
			sb.append("当前 "+this.getPageNow()+"/"+this.getPageCount()+" 页 &nbsp;转到第<input id=\"_goPs\" value="+this.getPageNow()+" style=\"width: 50px;\" onfocus=\"this.select();\" onkeypress=\"if(event.keyCode==13){$('#_goPage').click();return false;}\" type=\"text\">页");
			sb.append(" <input id=\"_goPage\" value=\"转\" onclick=\"_gotoPage($('#_goPs').val());\"  type=\"button\">");
			sb.append("<script type=\"text/javascript\">\n");
			sb.append("function _gotoPage(pageNo) {\n");
			sb.append("try{\n");
			sb.append("var tableForm = getTableForm();\n");
			sb.append("$(\"input[name=pageNo]\").val(pageNo);\n");
			sb.append("tableForm.action=\"v_list.do\";\n");
			sb.append("tableForm.onsubmit=null;\n");
			sb.append("tableForm.submit();\n");
			sb.append("} catch(e) {\n");
			sb.append("alert('_gotoPage(pageNo)方法出错');\n");
			sb.append("}\n");
			sb.append("}\n");
			sb.append("</script>\n");
		}
		pageStr = sb.toString();
		return pageStr;
	}

	public void setPageStr(String pageStr) {
		this.pageStr = pageStr;
	}
}
